package com.sh.one;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 模拟feign代理，简化执行过程
 */
public class RxClientProxy implements InvocationHandler {
 
    private String baseUrl;
    private String prefix;
 
    public RxClientProxy(String baseUrl, String prefix) {
        this.baseUrl = baseUrl;
        this.prefix = prefix;
    }
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RxGet annotation = method.getAnnotation(RxGet.class);
        String url = getUrl(annotation);
 
        // 模拟调用接口
        Object rs = mockGet(url);
 
        System.out.println(String.format("模拟调用接口：\nurl -> %s\nres -> %s\n", url, rs));
        return rs;
    }
 
    private Object mockGet(String url) {
        return url.contains("username")?"一上访人":"幼儿园丶抠脚上单";
    }
 
    private String getUrl(RxGet annotation) {
        return this.baseUrl + this.prefix + annotation.path();
    }
}